package com.crc.luojian.util;

import com.crc.luojian.annotation.Component;
import com.crc.luojian.annotation.Controller;
import com.crc.luojian.annotation.Service;
import com.crc.luojian.beans.model.BeanDefinition;
import com.crc.luojian.factory.impl.DefaultBeanFactory;
import org.apache.commons.lang3.StringUtils;


import java.util.Set;

/**
 * @program: luojian-springframework
 * @description:
 * @author: luojian58
 * @create: 2021-06-10 15:21
 */
public class ComponentBeanReader {
    /**
     * @Author luojian
     * @Description  将Service ，Controller 注解的beanDefinition 注入到map 中
     * @Date  2021/6/10 15:32
     * @Param [beanFactory]
     * @return void
     **/
    public void loadBeanDefinitions(DefaultBeanFactory beanFactory){
        Set<Class<?>> serviceSet = ReflectionUtils.getAllClass(Service.class);
        Set<Class<?>> controllerSet = ReflectionUtils.getAllClass(Controller.class);
        Set<Class<?>> componentSet = ReflectionUtils.getAllClass(Component.class);
        serviceSet.addAll(controllerSet);
        serviceSet.addAll(componentSet);
        BeanDefinition beanDefinition = null;
        String beanName = null;
        for(Class<?> classSet : serviceSet){
            Service annotation = classSet.getAnnotation(Service.class);
            //如果service 设置了bean 服务名那么用value 作为beanName
            if(annotation != null) {
                beanName = annotation.value();
                if(StringUtils.isEmpty(beanName)){
                    beanName = BeanUtils.lowerFirst(classSet.getSimpleName());
                }
            }else {
                beanName = BeanUtils.lowerFirst(classSet.getSimpleName());
            }
            beanDefinition = new BeanDefinition();
            beanDefinition.setBenaClass(classSet);
            beanFactory.registerBeanDefinition(beanName,beanDefinition);
        }
    }
}
